package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService{
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;

    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        if(strategyCatalog.getId() == null){
            if(strategyCatalog.getSequence() == null){
                Integer maxSequence = strategyCatalogMapper.selectMaxSequence(strategyCatalog.getStrategy().getId());
                if (maxSequence == null) {
                    maxSequence = 0;
                }
                maxSequence ++;
                strategyCatalog.setSequence(maxSequence);
            }
            strategyCatalogMapper.insert(strategyCatalog);
        }else {
            strategyCatalogMapper.updateByPrimaryKey(strategyCatalog);
        }
    }


    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyCatalog> list = strategyCatalogMapper.queryForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Map> listByStrategyId(Long strategyId) {

        return strategyCatalogMapper.selectByStrategyId(strategyId);
    }

}
